﻿using System;
using System.Threading;
using System.Web.UI;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine.Site;

namespace visinia.web
{
    public partial class Designer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Thread.CurrentPrincipal.IsInRole("designer") || (Thread.CurrentPrincipal.IsInRole("admin"))))
            {
                int PageId = 0;
                Int32.TryParse(Request.QueryString["PageId"], out PageId);
                if (WebModuleController.LoadModules(PageId, Page, WebModuleController.MODES.DESIGNER))
                {   //disable viewstate
                    EnableViewState = false;
                    AddScriptVariables();
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/jquery-1.3.2.min.js");
                    // for easy and light weight data transfer
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "json/jquery.json-2.2.min.js");
                    // dependencies for ui.dialog.js
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.core.js");
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.draggable.js");
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.resizable.js");
                    // for module properties dialog
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.dialog.js");
                    // for module repository tabs etc.
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.tabs.js");
                    // visinia dialog styles override
                    WebPageController.AddAdminThemeAwareCssLink(Page, "dialog.css");
                    // module settings dialog controle's styles
                    WebPageController.AddAdminThemeAwareCssLink(Page, "admin-form.css");
                    // for module drag and drop
                    WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.sortable.js");
                    // the visinia module drag drop framework
                    WebPageController.AddScriptLink(Page, WebRoots.GetAdminScriptsRoot + "designer/module.js");
                    // designer mode styles
                    WebPageController.AddAdminThemeAwareCssLink(Page, "module.css");
                    WebPageController.AddScriptLink(Page, WebRoots.GetAdminScriptsRoot + "Admin/jconfirmaction.jquery.js");
                    Page.DataBind();
                }
            }
            else
            {
                Response.Redirect(WebRoots.GetWebRoot);
            }
        }

        void Page_PreInit(Object sender, EventArgs e)
        {
            MasterPageFile = WebRoots.GetThemesRoot + SiteSettings.Theme + "/page.master";
        }

        private void AddScriptVariables()
        {
            string root = "<input type=\"hidden\" value=\"" + WebRoots.GetWebRoot + "\" id=\"WebRoots_GetWebRoot\" />";
            LiteralControl rootctrl = new LiteralControl(root);
            Page.Form.Controls.Add(rootctrl);
        }        
    }
}
